package edu.byu.intex2.data.dao;

import edu.byu.intex2.data.bo.Part; //What's this?
import edu.byu.intex2.data.Cache;
import edu.byu.intex2.data.ConnectionPool;
import edu.byu.intex2.data.DataException;
import java.sql.*;

/**
 * part Data Access Object
 * @author Adam Chavez
 */
public class PartDAO {

  /////////////////////////////////////////////
  ///   Singleton code

  private static PartDAO instance = null;

  /** Creates a new instance of PartDAO */
  private PartDAO() {
  }//constructor

  /** Retrieves the single instance of this class */
  public static synchronized PartDAO getInstance() {
    if (instance == null) {
      instance = new PartDAO();
    }
    return instance;
  }//getInstance

  ////////////////////////////////////////////
  ///   CREATE methods

  /**
   * Creates a new part in the database
   * set alreadyInDB to false
   * put part in cache
   */
  public PartDAO create(String id) throws DataException {

  }//create


  ////////////////////////////////////////////
  ///   READ methods

  /**
   * Reads an existing part from the database
   * 1) Checks cache
   * 2) Gets a connection
   * 3) Calls read with connection argument
   */
  public PartDAO read(String id) throws DataException {
  }

  /**
   * Internal method to read an existing part from the database
   * 1) Check cache for object
   * 2) Create a prepared statement to execute sql query
   * 3) Create a resultset from the preparedstatement
   * 4) Return the part
   */
  synchronized PartDAO read(String id, Connection conn) throws Exception {

  }//read


  /**
   * Internal method to create a part object from a record
   * 1) Check cache for object
   * 2) Instantiate new part
   * 3) Set alreadyInDb to false
   * 4) Set all variables from result set
   * 5) Set dirty to true
   * 6) Return part
   */
  synchronized PartDAO readRecord(ResultSet rs, Connection conn) throws Exception {
  }//readRecord

  /////////////////////////////////////////////
  ///   UPDATE methods

  /** Saves an existing part in the database */
  public void save(Part part) throws DataException {
    //get a connection from the pool, save all of the sales first using the other part method and then commit them in the database
	//rollback if there were any errors saving, catch any other errors you might have
	//release the connection!!

  }

  /** Internal method to update a part in the database */
  void save(Part part, Connection conn) throws Exception {
    //check cache first, if returns nothing, put the part object to cache
	//decide whether to update or insert into the database

    }

  /** Saves an existing part to the database */
  private void update(Part part, Connection conn) throws Exception {

	//use a prepared statement to prevent sql injection
	//run the query and set dirty to fase
	//make sure to close the prepared statement

  }

  /** Inserts a new part into the database */
  private void insert(Part part, Connection conn) throws Exception {
    //use a perpared statement
	//set the alreadyInDB variable to true
	//dirty to false
	//close the prepared statement

  }





  /////////////////////////////////////////////////
  ///   DELETE methods

  /** We do not support deleting of business objects in this application */
  public void delete(Part part) throws DataException {
    throw new RuntimeException("Nice try. The delete function is not supported in this application.");
  }



  ////////////////////////////////////////////////
  ///   SEARCH methods

  /** Retrieves all conceptualcars from the database */
  public List<Part> getAll() throws DataException {
    //establish a connection, use a prepared statement, plug it into the search method return a list of parts
    }
 }

  /** Internal method to search by certain fields */
